package com.manage.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.dto.ActivityDTO;
import com.manage.dto.add.ActivityAddDTO;
import com.manage.dto.update.ActivityUpdateDTO;
import com.manage.entity.Activity;
import com.manage.result.PageResult;
import com.manage.server.mapper.ActivityMapper;
import com.manage.server.service.ActivityService;
import com.manage.vo.ActivityVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Override
    public boolean save(ActivityAddDTO activityDTO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDTO, activity);
        return this.save(activity);
    }

    @Override
    public boolean updateById(ActivityUpdateDTO activityUpdateDTO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityUpdateDTO, activity);
        return super.updateById(activity);
    }

    @Override
    public PageResult<ActivityVO> findActivityByPage(ActivityDTO dto) {
        // 1. 处理分页参数（修正参数顺序问题）
        int pageNum = dto.getPageNum() != null && dto.getPageNum() > 0 ? dto.getPageNum() : 1;
        int pageSize = dto.getPageSize() != null && dto.getPageSize() > 0 ? dto.getPageSize() : 10;

        // 2. 创建分页对象（注意参数顺序：current, size）
        Page<Activity> page = new Page<>(pageNum, pageSize);

        // 3. 构建动态查询条件
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();

        // 字符串条件
        if (StringUtils.hasText(dto.getName())) {
            queryWrapper.like("name", dto.getName().trim());
        }

        // 精确匹配条件
        if (dto.getIsActive() != null) {
            queryWrapper.eq("is_active", dto.getIsActive());
        }
        if (dto.getCreatorId() != null) {
            queryWrapper.eq("creator_id", dto.getCreatorId());
        }
        if (dto.getLocationId() != null) {
            queryWrapper.eq("location_id", dto.getLocationId());
        }
        if (dto.getAllowSelfEnroll() != null) {
            queryWrapper.eq("allow_self_enroll", dto.getAllowSelfEnroll());
        }

        // 时间范围条件
        if (dto.getCreatedAt() != null) {
            queryWrapper.ge("created_at", dto.getCreatedAt());
        }
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.between("start_time", dto.getStartTime(), dto.getEndTime());
        } else {
            if (dto.getStartTime() != null) {
                queryWrapper.ge("start_time", dto.getStartTime());
            }
            if (dto.getEndTime() != null) {
                queryWrapper.le("end_time", dto.getEndTime());
            }
        }

        // 4. 执行分页查询
        Page<Activity> resultPage = this.page(page, queryWrapper);

        // 5. 转换Entity到VO（处理特殊字段）
        Page<ActivityVO> voPage = (Page<ActivityVO>) resultPage.convert(entity -> {
            ActivityVO vo = new ActivityVO();
            BeanUtils.copyProperties(entity, vo);
            // 处理数据库字段和VO字段不一致的情况（如果有）
            // vo.setXXX(entity.getXXX());
            return vo;
        });

        // 6. 返回分页结果
        return PageResult.success(voPage);
    }
}